package entity.JPanel;

import utils.JDBCUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Borrow_return extends JPanel {
    BufferedImage bg;
    private int width;
    private int height;
    //图书借还
    private JLabel jLabel1 = new JLabel();
    //请输入书名
    private JLabel jLabel2 = new JLabel();
    //书号
    private JLabel jLabel3 = new JLabel();
    //书名
    private JLabel jLabel4 = new JLabel();
    //作者
    private JLabel jLabel5 = new JLabel();
    //类型
    private JLabel jLabel6 = new JLabel();
    //库存
    private JLabel jLabel7 = new JLabel();
    //书号内容
    private JLabel jLabel8 = new JLabel();
    //书名内容
    private JLabel jLabel9 = new JLabel();
    //作者内容
    private JLabel jLabel10 = new JLabel();
    //类型内容
    private JLabel jLabel11 = new JLabel();
    //库存内容
    private JLabel jLabel12 = new JLabel();
    //搜索输入框
    private JTextField jTextField1 = new JTextField();
    //搜索按钮
    private JButton jButton1;
    //数组
    private String a[];
    //借书按钮
    private JButton jButton2 = new JButton("借书");
    private JButton jButton3 = new JButton("还书");
    static Connection con = null;
    static PreparedStatement preSql;
    static ResultSet rs;
    private boolean key = false;

    public Borrow_return(int width, int height) {
        this.width = width;
        this.height = height;
        setLayout(null);
        initComponents();
        try {
            bg = ImageIO.read(UserLoginJPanel.class.getClassLoader().getResourceAsStream("resources/images/land.jpg"));
        } catch (Exception e) {
            System.out.println("NULL");
        }
    }

    private void initComponents() {
        //-----------jLabel1-----------
        //设置jLabel1大小位置
        jLabel1.setBounds(850, 80, 300, 80);
        //定义
        Font font1 = new Font("华文琥珀", Font.PLAIN, 60);
        //设置字体颜色为black。
        jLabel1.setForeground(Color.black);
        //设置字体和大小。
        jLabel1.setFont(font1);
        jLabel1.setText("图书借还");
        add(jLabel1);
        //----------jLabel2----------
        //设置jLabel1大小位置
        jLabel2.setBounds(650, 190, 300, 60);
        //设置字体颜色为black。
        jLabel2.setForeground(Color.black);
        //jLabel2设置字体。
        jLabel2.setFont(new Font("宋体", Font.PLAIN, 30));
        //设置jLabel2内容
        jLabel2.setText("输入图书书号:");
        //添加jLabel2到面板
        add(jLabel2);
        //-----------jTextField1-----------
        //设置jTextField1大小位置
        jTextField1.setBounds(850, 200, 300, 45);
        //设置jTextField1字体
        jTextField1.setFont(new Font("宋体", Font.BOLD, 30));
        //添加jTextField1到面板
        add(jTextField1);
        //-----------jButton1--------------
        //设置按钮内容
        jButton1 = new JButton("检索");
        //设置按钮位置大小
        jButton1.setBounds(1200, 200, 120, 45);
        //设置按钮颜色
        jButton1.setBackground(new Color(255, 228, 196));
        //设置按钮图标
        jButton1.setIcon(new ImageIcon(Borrow_return.class.getClassLoader().getResource("resources/images/sreach.png").getPath()));
        //按钮设置字体样式
        jButton1.setFont(new Font("楷体", Font.PLAIN, 20));
        //按钮失去边框
        jButton1.setBorderPainted(false);
        //按钮失去焦点
        jButton1.setFocusPainted(false);
        //添加jButton1到面板
        add(jButton1);
        jButton1.addActionListener((e) -> {
            boolean flag = false;
            try {
                con = JDBCUtil.getConnect();
                String sqlStr = "select * from books_table where id=?";
                System.out.println("id检索con");
                preSql = con.prepareStatement(sqlStr);
                preSql.setString(1, jTextField1.getText());
                rs = preSql.executeQuery();
                while (rs.next()) {
                    flag = true;
                    key = true;
                    jLabel8.setText(rs.getString("id"));
                    jLabel9.setText(rs.getString("name"));
                    jLabel10.setText(rs.getString("author"));
                    jLabel11.setText(rs.getString("type"));
                    jLabel12.setText(rs.getString("num"));
                }
                if (!flag) {
                    jLabel8.setText("");
                    jLabel9.setText("");
                    jLabel10.setText("");
                    jLabel11.setText("");
                    jLabel12.setText("");
                    key = false;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                JDBCUtil.close(preSql, rs, con);
                System.out.println("书号检索完毕");
            }
        });
        //-------------jLabel3----------
        jLabel3.setBounds(780, 300, 1000, 45);
        jLabel3.setText("书号：");
        jLabel3.setFont(new Font("宋体", Font.BOLD, 30));
        add(jLabel3);
        //-------------jLabel4-----------
        jLabel4.setBounds(780, 350, 1000, 45);
        jLabel4.setText("书名：");
        jLabel4.setFont(new Font("宋体", Font.BOLD, 30));
        add(jLabel4);
        //-------------jLabel5-----------
        jLabel5.setBounds(780, 400, 1000, 45);
        jLabel5.setText("作者：");
        jLabel5.setFont(new Font("宋体", Font.BOLD, 30));
        add(jLabel5);
        //-------------jLabel6-----------
        jLabel6.setBounds(780, 450, 1000, 45);
        jLabel6.setText("类型：");
        jLabel6.setFont(new Font("宋体", Font.BOLD, 30));
        add(jLabel6);
        //-------------jLabel7-----------
        jLabel7.setBounds(780, 500, 1000, 45);
        jLabel7.setText("库存：");
        jLabel7.setFont(new Font("宋体", Font.BOLD, 30));
        add(jLabel7);
        //-------------jLabel8-----------
        jLabel8.setBounds(860, 300, 1000, 45);
        jLabel8.setFont(new Font("宋体", Font.BOLD, 30));
        add(jLabel8);
        //-------------jLabel9-----------
        jLabel9.setBounds(860, 350, 1000, 45);
        jLabel9.setFont(new Font("宋体", Font.BOLD, 30));
        add(jLabel9);
        //-------------jLabel10-----------
        jLabel10.setBounds(860, 400, 1000, 45);
        jLabel10.setFont(new Font("宋体", Font.BOLD, 30));
        add(jLabel10);
        //-------------jLabel11-----------
        jLabel11.setBounds(860, 450, 1000, 45);
        jLabel11.setFont(new Font("宋体", Font.BOLD, 30));
        add(jLabel11);
        //-------------jLabel12-----------
        jLabel12.setBounds(860, 500, 1000, 45);
        jLabel12.setFont(new Font("宋体", Font.BOLD, 30));
        add(jLabel12);
        //----------jbutton2-----------
        jButton2.setBounds(750, 600, 100, 50);
        //设置按钮颜色
        jButton2.setBackground(new Color(135, 206, 235));
        //按钮设置字体样式
        jButton2.setFont(new Font("黑体", Font.PLAIN, 20));
        //按钮失去边框
        jButton2.setBorderPainted(false);
        //按钮失去焦点
        jButton2.setFocusPainted(false);
        add(jButton2);
        //----------jbutton2-----------
        jButton3.setBounds(1200, 600, 100, 50);
        //按钮设置字体样式
        jButton3.setFont(new Font("黑体", Font.PLAIN, 20));
        //设置按钮颜色
        jButton3.setBackground(new Color(135, 206, 235));
        //按钮失去边框
        jButton3.setBorderPainted(false);
        //按钮失去焦点
        jButton3.setFocusPainted(false);
        add(jButton3);
        /**
         * 借书监听器
         */
        jButton2.addActionListener((e) -> {
            if (key) {
                con = JDBCUtil.getConnect();
                int rtn = JOptionPane.showConfirmDialog(null, "是否借书", "借书", JOptionPane.YES_NO_OPTION);
                if (rtn == JOptionPane.YES_OPTION) {
                    try {
                        String sql1 = "select * from books_table where id=?";
                        preSql = con.prepareStatement(sql1);
                        preSql.setString(1, jTextField1.getText());
                        rs = preSql.executeQuery();
                        while (rs.next()){
                            if( Integer.valueOf(rs.getString("num"))>0){
                                String sql = "update books_table set num=num-1 where id=?";
                                preSql = con.prepareStatement(sql);
                                preSql.setString(1, jTextField1.getText());
                                int results = preSql.executeUpdate();
                                System.out.println("res+" + results);
                                preSql = con.prepareStatement(sql1);
                                preSql.setString(1, jTextField1.getText());
                                rs = preSql.executeQuery();
                                while (rs.next()) {
                                    jLabel12.setText(rs.getString("num"));
                                }
                            }
                            else{
                                JOptionPane.showMessageDialog(null, "库存不足", "fail", JOptionPane.WARNING_MESSAGE);

                                System.out.println("no");
                            }
                        }


                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    } finally {
                        JDBCUtil.close(preSql, rs, con);
                    }
                }
            }
        });
        /**
         * 还书监听器
         */
        jButton3.addActionListener((e) -> {
            if (key) {
                int rtn = JOptionPane.showConfirmDialog(null, "是否还书", "还书", JOptionPane.YES_NO_OPTION);
                if (rtn == JOptionPane.YES_OPTION) {
                    try {
                        con = JDBCUtil.getConnect();
                        String sql = "update books_table set num=num+1 where id=?";
                        preSql = con.prepareStatement(sql);
                        preSql.setString(1, jTextField1.getText());
                        int results = preSql.executeUpdate();
                        System.out.println("res+" + results);
                        String sql1 = "select * from books_table where id=?";
                        preSql = con.prepareStatement(sql1);
                        preSql.setString(1, jTextField1.getText());
                        rs = preSql.executeQuery();
                        while (rs.next()) {
                            jLabel12.setText(rs.getString("num"));
                        }
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    } finally {
                        JDBCUtil.close(preSql, rs, con);
                    }
                }
            }
        });
    }

    public void paintComponent(Graphics g) {
        g.drawImage(bg, 0, 0, width, height, null);//绘制背景图片
    }
}

